/**
 * @Title:  AttentionDynamicsActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2015-12-7 上午10:20:14 
 * @version:  V1.0 
 */

package artid.com.arttogo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.volley.VolleyError;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.adapter.AttentionFollowAdapter;
import artid.com.arttogo.bean.ArtistList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class MyFollowActivity extends BaseFragment{
	private View mView;
	PullToRefreshLayout mPullLayout;
	PullableListView mlistview;
	int pagesize = 50,pageIndex = 1;
	AttentionFollowAdapter adapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.attention_follow_layout, null);
		mlistview = (PullableListView)mView.findViewById(R.id.listivew);
		mPullLayout = (PullToRefreshLayout)mView.findViewById(R.id.refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

					@Override
					public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
						// TODO Auto-generated method stub
						pageIndex = 1;
						reqData("");
					}

					@Override
					public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
						// TODO Auto-generated method stub
						// pullToRefreshLayout.setBottomDis3miss();
						pageIndex++;
						reqData("");
					}
				});
		
		return mView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		reqData("");
	}
	void reqData(final String keywork) {
		
		VolleyBasic.getInstance(getActivity()).post(
				Cache.artistList,
				VolleyMap.getInstance().artistList("", String.valueOf(pageIndex), String.valueOf(pagesize), keywork,
						AppUtil.getUser().getToken()), ArtistList.class,
				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
							mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
							final List<ArtistList> list = (List<ArtistList>) vd
									.getList();
							if (list.isEmpty()) {
								return;
							}
							if (pageIndex!=1) {
								adapter.addData(list);
								setItemClick(adapter.mList);
								return;
							}
							adapter = new AttentionFollowAdapter(getActivity(),list);
							mlistview.setAdapter(adapter);
							setItemClick(adapter.mList);
							
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}
	
	void setItemClick(final List<ArtistList> list){
		mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						getActivity(),
					CollecterActivity.class);
				intent.putExtra("id", list.get(arg2).getId());
				startActivity(intent);
			}
		});
		
	}
}
